﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MailSender.Data;
using System.Xml.Linq;

namespace MailSender
{
    public partial class ToAddressManage : Form
    {
        public ToAddressManage()
        {
            InitializeComponent();
        }

        private List<XElement> GetXElements()
        {
            XDocument doc = XDocument.Load(SysConstants.ToAddressFile);
            XElement config = doc.Element("config");
            List<XElement> toadds = config.Elements("toadd").ToList<XElement>();
            return toadds;
        }


        private List<ToAddress> GetToAddresses(List<XElement> xes)
        {
            List<ToAddress> allTos = new List<ToAddress>();

            if (xes.Count > 0)
            {
                foreach (var item in xes)
                {
                    ToAddress toAddress = new ToAddress();
                    toAddress.Type = item.Attribute("type").Value;
                    toAddress.Email = item.Attribute("email").Value;
                    allTos.Add(toAddress);
                }
            }
            return allTos;
        }

        private void ToAddressManage_Load(object sender, EventArgs e)
        {
            LoadDataGridView();
        }

        public void LoadDataGridView()
        {
            ToAddressDataGridView.DataSource = GetToAddresses(GetXElements());
            Numlabel.Text = GetToAddresses(GetXElements()).Count.ToString();
        }

        private void AddToAddBt_Click(object sender, EventArgs e)
        {
            AddToAddress addTo = new AddToAddress(this);
            addTo.ShowDialog();
        }

        private void DeleteTypeBt_Click(object sender, EventArgs e)
        {
            if (ToAddressDataGridView.SelectedRows.Count == 0)
            {
                MessageBox.Show("至少选择一个");
                return;
            }

            string email = (string)((ToAddressDataGridView.SelectedRows)[0]).Cells[0].Value;
            string type = (string)((ToAddressDataGridView.SelectedRows)[0]).Cells[1].Value;

            XDocument doc = XDocument.Load(SysConstants.ToAddressFile);
            XElement config = doc.Element("config");
            List<XElement> toadds = config.Elements("toadd").ToList<XElement>();
            if (toadds.Count > 0)
            {
                XElement element = toadds.Where(c => c.Attribute("type").Value == type).Where(c => c.Attribute("email").Value == email).SingleOrDefault();
                if (element != null)
                {
                    element.Remove();
                }
            }
            doc.Save(SysConstants.ToAddressFile);
            LoadDataGridView();
        }

        private void SelectTypeBt_Click(object sender, EventArgs e)
        {
            string email = EmailTxt.Text;
            string type = TypeComboBox.Text;
            XDocument doc = XDocument.Load(SysConstants.ToAddressFile);
            XElement config = doc.Element("config");
            List<XElement> toadds = config.Elements("toadd").ToList<XElement>();
            if (toadds.Count > 0)
            {
                if (!string.IsNullOrEmpty(email))
                {
                    toadds = toadds.Where(c => c.Attribute("email").Value == email).ToList();
                }
                if (!string.IsNullOrEmpty(type))
                {
                    toadds = toadds.Where(c => c.Attribute("type").Value == type).ToList();
                }
            }
            ToAddressDataGridView.DataSource = GetToAddresses(toadds);
            Numlabel.Text = toadds.Count.ToString();
        }
    }
}
